package com.chixing.Interceptor;

import com.chixing.utils.TokenUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if(request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }

        String token = null;
        if((token = request.getParameter("token") )!=null || (token = request.getHeader("token")) !=null){
            System.out.println("通过拦截器！");
            return true;
        }else {
            System.out.println("认证失败，未通过拦截器！");
            response.sendRedirect(request.getContextPath()+"/personal/login.jsp"); //未通过，先完成登录
            return false;
        }

    }
}